package com.example.koizzi.ad20180606_note;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.ArrayList;

public class StuAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<student> stuArrayListData;
    private DelStuData delStuData;

    public StuAdapter(Context context, ArrayList<student> stuArrayListData, DelStuData delStuData) {
        this.context = context;
        this.stuArrayListData = stuArrayListData;
        this.delStuData = delStuData;
    }

    @Override
    public int getCount() {
        return stuArrayListData.size();
    }

    @Override
    public Object getItem(int i) {
        return stuArrayListData.get(i);
    }


    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.stu_item, viewGroup, false);

        }
        MyHolder holder = new MyHolder();
        final student s = stuArrayListData.get(i);

        holder.tvStuId = view.findViewById(R.id.tv_stuid);
        holder.tvStuName = view.findViewById(R.id.tv_stuname);
        holder.tvStuAge = view.findViewById(R.id.tv_stuage);
        holder.btnDelStu = view.findViewById(R.id.btn_delstu);

        holder.tvStuId.setText(String.valueOf(s.getId()));
        holder.tvStuName.setText(s.getName());
        holder.tvStuAge.setText(String.valueOf(s.getAge()));
        holder.btnDelStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {//点击删除
                int id = s.getId();
                delStuData.delStuData(id);
            }
        });
        return view;
    }

    class MyHolder {
        TextView tvStuId;
        TextView tvStuName;
        TextView tvStuAge;
        ImageButton btnDelStu;
    }
}
